package com.windystory.wslibrary.ui.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;

import com.windystory.wslibrary.R;

/**
 * Button 基于 AntDesign
 *
 * <pre>
 *  示例
 *  <com.league.app.antdesignlibrary.ui.button.Button
 *         android:layout_width="wrap_content"
 *         android:layout_height="35dp"
 *         android:layout_margin="10dp"
 *         android:text="XXX"
 *         android:gravity="center"
 *         android:textColor="@color/white"
 *         android:textSize="12sp"
 *         app:borderSizeBtn="10"
 *         app:pressBorderColorBtn="@color/colorAccent"
 *         app:unPressBorderColorBtn="@color/colorAccent"
 *         app:pressColorBtn="@color/colorAccent"
 *         app:styleButton="circle|normal|tricircle|dashLine"   dashLine 虚线按钮 normal四方  tircircle圆角 circle更圆角
 *         app:unPressColorBtn="@color/colorPrimary" />
 *
 *
 *  蓝色按钮
 *  <com.league.app.antdesignlibrary.ui.button.Button
 *         android:layout_width="wrap_content"
 *         android:layout_height="35dp"
 *         android:layout_margin="10dp"
 *         android:translationZ="1dp"
 *         android:text="XXX"
 *         android:gravity="center"
 *         android:textColor="@color/white"
 *         android:textSize="12sp"
 *         app:pressColorBtn="#166CDF"
 *         app:styleButton="circle"
 *         app:unPressColorBtn="#2490FF"
 *         tools:ignore="UnusedAttribute" />
 * </pre>
 *
 * @author windystory
 * @version 1.0
 */
public class Button extends androidx.appcompat.widget.AppCompatButton {

    //正常
    private final int NORMAL = 0;
    //虚线
    private final int DASHLINE = 1;
    //圆角矩形
    private final int TRICIRCLE = 2;
    //圆角
    private final int CIRCLE = 3;

    //按钮类型
    private int BUTTON_STYLE = NORMAL;
    //未按下颜色
    private int unPressColor;
    //按下按钮颜色
    private int pressColor;
    //边框颜色
    private int unPressBorderColor = -1;
    private int pressBorderColor = -1;
    //圆角矩形圆角角度大小dp
    private final int TRI_RADIAN = 15;
    //圆形圆角角度大小dp
    private final int RADIAN = 55;
    //边框大小
    private int border;

    //虚线边框粗细
    private int dashLineWidth;
    //虚线边框间距
    private int dashLineGap;
    private static final int UNSPECIFIED_INT = Integer.MAX_VALUE;

    public Button(Context context) {
        super(context);
    }

    public Button(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Button);
            BUTTON_STYLE = a.getInt(R.styleable.Button_styleButton, NORMAL);
            border = a.getInt(R.styleable.Button_borderSizeBtn, 1);
            unPressColor = a.getColor(R.styleable.Button_unPressColorBtn, UNSPECIFIED_INT);
            pressColor = a.getColor(R.styleable.Button_pressColorBtn, UNSPECIFIED_INT);
            unPressBorderColor = a.getColor(R.styleable.Button_unPressBorderColorBtn, UNSPECIFIED_INT);
            pressBorderColor = a.getColor(R.styleable.Button_pressBorderColorBtn, UNSPECIFIED_INT);
            dashLineWidth = a.getInt(R.styleable.Button_dashLineWidthBtn, 8);
            dashLineGap = a.getInt(R.styleable.Button_dashLineGapBtn, 8);
            setButton(BUTTON_STYLE);
        }
    }

    /**
     * 设置按钮样式
     *
     * @param button_style
     */
    private void setButton(int button_style) {
        switch (button_style) {
            case DASHLINE://虚线类型 简单封装，使用频率不高
                btnStyle(DASHLINE, border, unPressBorderColor, pressBorderColor, pressColor, unPressColor, 15, dashLineWidth, dashLineGap, true, true);
                break;
            case TRICIRCLE://圆角矩形
                btnStyle(TRICIRCLE, border, unPressBorderColor, pressBorderColor, pressColor, unPressColor, TRI_RADIAN, 0, 0, true, false);
                break;
            case CIRCLE://圆角
                btnStyle(CIRCLE, border, unPressBorderColor, pressBorderColor, pressColor, unPressColor, RADIAN, 0, 0, true, false);
                break;
            default://正常类型
                btnStyle(NORMAL, border, unPressBorderColor, pressBorderColor, pressColor, unPressColor, 0, 0, 0, false, false);
                break;
        }
    }


    /**
     * 按钮类型
     *
     * @param style              样式类型
     * @param border             边框大小
     * @param unPressBorderColor 未按下边框颜色
     * @param pressBorderColor   按下边框颜色
     * @param pressColor         按下颜色
     * @param unPressColor       未按下颜色
     * @param RADIAN             弧度
     * @param dashLineWidth      虚线粗细
     * @param dashLineGap        虚线间距
     * @param withRadian         是否开启圆角
     * @param dashLine           是否开启虚线
     */
    private void btnStyle(int style, int border, int unPressBorderColor, int pressBorderColor, int pressColor, int unPressColor, int RADIAN, int dashLineWidth, int dashLineGap, boolean withRadian, boolean dashLine) {

        pressBorderColor = pressBorderColor == -1 ? Color.TRANSPARENT : pressBorderColor;
        unPressBorderColor = unPressBorderColor == -1 ? Color.TRANSPARENT : unPressBorderColor;

        setBackgroundColor(unPressColor);

        GradientDrawable normal = new GradientDrawable();
        normal.setColor(unPressColor);

        GradientDrawable pressed = new GradientDrawable();
        pressed.setColor(pressColor);

        if (withRadian) {
            normal.setCornerRadius(RADIAN);
            pressed.setCornerRadius(RADIAN);
            normal.setStroke(border, unPressBorderColor);
            pressed.setStroke(border, pressBorderColor);
        }
        if (!dashLine) {
            normal.setStroke(border, unPressBorderColor);
            pressed.setStroke(border, pressBorderColor);
        } else {
            normal.setStroke(border, unPressBorderColor, dashLineWidth, dashLineGap);
            pressed.setStroke(border, pressBorderColor, dashLineWidth, dashLineGap);
        }


        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateListDrawable.addState(new int[]{android.R.attr.state_focused}, pressed);
        stateListDrawable.addState(new int[]{}, normal);
        setBackgroundDrawable(stateListDrawable);

    }
}
